package com.demo.camunda.util;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@EnableAsync
@Configuration
public class MyTreadPool {


    private static int core = Runtime.getRuntime().availableProcessors();

    /**
     * 核心线程
     */
    private static int corePoolSize = core + 1;

    /**
     * 最大线程
     */
    private static int maxPoolSize = core * 2;

    /**
     * 队列容量
     */
    private int queueCapacity = 4096;

    /**
     * 保持时间
     */
    private int keepAliveSeconds = 90;

    /**
     * 名称前缀
     */
    private String preFix = "excelUpload-pool-";


    //
    @Bean("myExecutor")
    public Executor myExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(keepAliveSeconds);
        executor.setThreadNamePrefix(preFix);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }
////        ExecutorService executor = new ThreadPoolExecutor(corePoolSize,
////                maxPoolSize,
////                90,
////                TimeUnit.SECONDS,
////                new LinkedBlockingQueue<Runnable>(4096),
////                new CustomizableThreadFactory("上传excel线程池-pool-"),
////                new ThreadPoolExecutor.AbortPolicy());
//
//        return executor;
//    }


}
